package project;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import project.gui.MainFrame;

import jade.core.Agent;
import jade.core.behaviours.SimpleBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;
import jess.JessException;

public class Agent4 extends Agent {

	private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

	private List<List<DateFiltrate>> lists;

	protected void setup() {
		
		MainFrame frame = new MainFrame();
		frame.pack();
		frame.setVisible(true);
		
		propertyChangeSupport.addPropertyChangeListener(frame);

		addBehaviour(new SimpleBehaviour(this) {

			public void action() {

				while (Ag12.bool) {

					if (Ag34.listaDePosibilitati != null) {
//						PrintSolution(Ag34.listaDePosibilitati);
						setLists(Ag34.listaDePosibilitati);
						Ag34.listaDePosibilitati = null;
					} else {
						if (Ag12.bool != true) {

							ACLMessage msg = receive();

							if (msg != null) {
								try {
									System.out.println("AG4 - " + myAgent.getLocalName() + " <- " + msg.getContentObject());
								} catch (UnreadableException e) {
									System.out.println("Exception in Agent3");
									e.printStackTrace();
								}

								try {
									System.out.println("AG4 - " + myAgent.getLocalName() + " <- " + msg.getContentObject());
								} catch (UnreadableException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}

								System.out.println("RECEIVED !!");
							}
						}
					}
				}
			}

			@Override
			public boolean done() {
				// TODO Auto-generated method stub
				return false;
			}

		});

	}

	public void setLists(List<List<DateFiltrate>> lists) {
		propertyChangeSupport.firePropertyChange("lists", this.lists, lists);
		this.lists = lists;
	}

	private void PrintSolution(List<List<DateFiltrate>> list) {
		System.out.println("done");
		if (list.isEmpty()) {
			System.out.println("pana de curent");
			return;
		}

		int[] penalizari = new int[list.size()];

		Reguli reguli = new Reguli();

		for (int i = 0; i < list.size(); i++) {
			for (DateFiltrate element : list.get(i)) {
				try {
					penalizari[i] += reguli.aplicaRegula(element.getAparat(), element.getTime().toString());
				} catch (JessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		System.out.println("Lista cu cea mai mare probabilitate de aparitie este:");

		int max = 0;
		for (int i = 0; i < list.size(); i++) {
			if (penalizari[i] > max)
				max = penalizari[i];
		}
		for (int i = 0; i < list.size(); i++) {
			if (penalizari[i] == max) {
				for (DateFiltrate element : list.get(i)) {
					System.out.println(element.toString());
				}
				break;
			}
		}
	}
}
